package com.filter;
import javax.servlet.*;
import java.io.IOException;

public class EncodingFilter implements Filter {
    private String encoding="utf-8";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        encoding = filterConfig.getInitParameter("encoding");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if (encoding != null) {
            //解决POST请求乱码
            servletRequest.setCharacterEncoding(encoding);
            //解决响应乱码
            servletResponse.setContentType("text/html;charset=" + encoding);
        }
        //放行
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
    }


}
